<?php
/**
*@package TMod.cls.php
*@copyright Copyright (c) 2012-2014 Tuniu.com
*@author huxiaomin 2012  下午02:35:47
*@version 1.0
*/

class TMod{
    //static $cache=array();
	
	private  $mod;
	private  $cache = array();
	public   $cache_back = array();
	//缓存文件夹
	static $folders = array();
	
	function __construct($class_name){
		if (empty($class_name)){
		  return '';
		}
		$this->init($class_name);
	}
	
	function init($class_name){
	   if (!class_exists($class_name)){
            $return = $this->load($class_name);
            if (!$return){
                die('class not exists');
            }
        }
        $this->mod = new $class_name();
	}

	function load($class_name){
		$class_name = substr($class_name,0,-3);
    	$dir = TMOD_DIR;
    	if (file_exists($dir.$class_name.TMOD_SUFFIX)){
    	   require_once $dir.$class_name.TMOD_SUFFIX;
    	   return true;
    	}
    	else {
    	   $dirs_arr = self::getFolder($dir);
    	   foreach ($dirs_arr as $v){
    	       if (file_exists($v.'/'.$class_name.TMOD_SUFFIX)){
    	           require_once $v.'/'.$class_name.TMOD_SUFFIX;
    	           return true;
    	       }
    	   }
    	}
    	return false;
    	
    }
    
    /**
     * 返回文件夹 
     * @param string $dir
     * @return array
     */
    static function getFolder($dir){
        if (self::$folders){
            return self::$folders;
        }
        
    	$TMemcache = new TMemcache();
    	$gCacheKey = gCacheKey($dir);
    	$cache_value = $TMemcache->get($gCacheKey);
    	if ($cache_value){
    		self::$folders = $cache_value;
    	   return $cache_value;
    	}
        
    	$res = opendir($dir);
        $return = array();
        if (!$res){
            return $return;
        }   
        
        while ($f = readdir($res)){
            if (is_dir($dir.$f) && $f{0} !== '.'){
                $return[] = $dir.$f;
            }
        }
        self::$folders = $return;
        
        $TMemcache->set($gCacheKey,$return,0,3600*24);
        return $return;
    }
    
    function __get($name){
        return $this->mod->$name;
    }
    
    function __set($name,$value){
        return $this->mod->$name = $value;
    }
    
    function beginMemcacheOnce($flag=0,$expire=1800,$refresh=0){
        $this->cache = array($flag,$expire,$refresh);
    }
    
    function openCache($flag=0,$expire=1800,$refresh=0){
        $this->cache = $this->cache_back = array($flag,$expire,$refresh);
    }
    
    function closeCache(){
        $this->cache = $this->cache_back = array();
    }
    
    function __call($name, $params){
    	$args = array(&$this->mod,&$name,&$params);
    	TPlugin::start('Tmod',$args);
    	/**
    	 * 这个可以添加mod的插件扩展。
    	 */
        if ($this->cache){
           $reflection = new ReflectionClass($this->mod);
           $file_name = $reflection->getFileName();
           $file_name = basename($file_name);
           $class_name = $reflection->getName();
           $key = "{$file_name}:{$class_name}.{$name}?".json_encode($params);
           $memcache = new TMemcache();
           if ($this->cache[2]){
            $memcache->delete($key);
           }
           else {
            $value = $memcache->get($key);
            if ($value){
              return $value;  
            }
           }
           
        }
    	
    	$return = call_user_func_array(array($this->mod,$name), $params);
    	
    	if ($this->cache){
    	   $memcache->set($key,$return,$this->cache[0],$this->cache[1]);
    	}
    	$this->cache = $this->cache_back;
    	return $return;
    	
    	
    }
    
}